﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
using BUS;
using System.Data;
using System.Data.SqlClient;
namespace QuanLyDiemSV
{
    public partial class QLDiemTrenLop : System.Web.UI.Page
    {
        ketnoi kn = new ketnoi();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                string magiangvien = "GV01";
                DataTable dt = BUS.QLDiemTrenLop.HienThiTen(magiangvien);
                DropDownList1.DataSource = dt;
                DropDownList1.DataValueField = "TenMonHoc";
                DropDownList1.DataTextField = "TenMonHoc";
                DropDownList1.DataBind();
            }
        }
        static string mamonhoc;
        private void load()
        {
            GridView1.DataSource = BUS.QLDiemTrenLop.xemdiem(mamonhoc);
            GridView1.DataBind();
        }
       
        protected void Button1_Click(object sender, EventArgs e)
        {
            TextBox txtmasv = (TextBox)GridView1.FooterRow.FindControl("txtmasv");
            TextBox txthoten = (TextBox)GridView1.FooterRow.FindControl("txthoten");
            TextBox txtdiem1 = (TextBox)GridView1.FooterRow.FindControl("txtdiem1");
            TextBox txtdiem2 = (TextBox)GridView1.FooterRow.FindControl("txtdiem2");
            TextBox txtdiem3 = (TextBox)GridView1.FooterRow.FindControl("txtdiem3");
            TextBox txtdiem4 = (TextBox)GridView1.FooterRow.FindControl("txtdiem4");
            TextBox txtdiem5 = (TextBox)GridView1.FooterRow.FindControl("txtdiem5");
            TextBox txtdghp = (TextBox)GridView1.FooterRow.FindControl("txtdghp");
            TextBox txtstn = (TextBox)GridView1.FooterRow.FindControl("txtstn");
            TextBox txtdcc = (TextBox)GridView1.FooterRow.FindControl("txtdcc");
            string masv=txtmasv.Text;
            string hoten=txthoten.Text;
            float diem1 = float.Parse(txtdiem1.Text);
            float diem2 = float.Parse(txtdiem2.Text);
            float diem3 = float.Parse(txtdiem3.Text);
            float diem4 = float.Parse(txtdiem4.Text);
            float diem5 = float.Parse(txtdiem5.Text);
            float diemgiuahocphan = float.Parse(txtdghp.Text);
            float sotietnghi = float.Parse(txtstn.Text);
            float diemchuyencan = float.Parse(txtdcc.Text);
            string hocky="1";
            string kq = kn.update("insert into DiemTrenLop values('" + masv + "','" + hoten + "','" + hocky + "','" + diem1 + "','" + diem2 + "','" + diem3 + "','" + diem4 + "','" + diem5 + "','" + diemgiuahocphan + "','" + sotietnghi + "','" + diemchuyencan + "')");
            if (kq != "")
            {
                Response.Write("<script>alert('ban them thanh cong');</script");
                load();
                
            }
            else
            {
                Response.Write("<script>alert('ban them that bai');</script");
            }
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string masv = e.Values["MaSinhVien"].ToString();
            string kq = kn.update("delete from DiemTrenLop where MaSinhVien = " + masv);
            if (kq != "")
            {
                Response.Write("<script>alert('ban xoa thanh cong');</script");
                load();
                
            }
            else
            {
                Response.Write("<script>alert('ban xoa that bai');</script");
            }
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            load();
            
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string masv = e.NewValues["MaSinhVien"].ToString();
            float diem1 =float.Parse(e.NewValues["Diem1"].ToString());
            float diem2 = float.Parse(e.NewValues["Diem2"].ToString());
            float diem3 =float.Parse(e.NewValues["Diem3"].ToString());
            float diem4 =float.Parse(e.NewValues["Diem4"].ToString());
            float diem5 =float.Parse(e.NewValues["Diem5"].ToString());
            float diemgiuahocphan = float.Parse(e.NewValues["DiemGiuaHocPhan"].ToString());
            float diemchuyencan = float.Parse(e.NewValues["DiemChuyenCan"].ToString());
            float sotietnghi = float.Parse(e.NewValues["SoTietNghi"].ToString());
            float diemtrungbinh = (diem1 + diem2 + diem3 + diem4 + diem5 + diemchuyencan + diemgiuahocphan * 2) / 8;
            string dieukienthi="";
            //int dkthi = BUS.QLDiemTrenLop.SoTrinh(mamonhoc)*15*3/100;
            if ((diemtrungbinh < 4))
            { 
                dieukienthi = "Học lại";
            }
            else
            {
                dieukienthi = "Đủ điều kiện";
            }
            string kq = kn.update("update DiemTrenLop set Diem1='" + diem1 + "',Diem2='" + diem2 + "',Diem3='" + diem3 + "',Diem4='" + diem4 + "',Diem5='" + diem5 + "',DiemGiuaHocPhan='" + diemgiuahocphan + "',DiemChuyenCan='" + diemchuyencan + "',DiemTrungBinh='" + diemtrungbinh+"',SoTietNghi='" + sotietnghi +"',DieuKienThi='" + dieukienthi+ "' where MaSinhVien='" + masv + "'");

            //string kq = BUS.QLDiemTrenLop.update("M01", masv, "HK01", diem1, diem2, diem3, diem4, diem5, diemgiuahocphan, diemchuyencan, diemtrungbinh, sotietnghi, dieukienthi);
            if(kq!="")
            {
                Response.Write("<script>alert('ban update thanh cong');</script");
                GridView1.EditIndex = -1;
                load();
            }
            else
            {
                Response.Write("<script>alert('ban update that bai');</script");
            }
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            load();
        }

        protected void Tìm_Click(object sender, EventArgs e)
        {
            mamonhoc = BUS.QLDiemTrenLop.LayMaMonHoc(DropDownList1.Text);
            load();
        }
    }
}